package com.syxbbs.forum.Interceptor;

import com.syxbbs.forum.config.AppConfig;
import com.syxbbs.forum.model.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
/*
* 登录拦截器
*
*
*
* */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    /*
    * 对请求的前置处理，true继续处理，false流程中断。*/
    //配置文件
    @Value("${syxbbs-forum.login.url}")
    private String defaultURl;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //true  表示放行   false表示拦截
        //获取session, 并且判断session中存储的userinfo信息是否为空
        HttpSession session = request.getSession();
        User user= (User)session.getAttribute(AppConfig.USER_SESSION);
        if (user==null || user.getId()<=0){
            log.info("LoginInterceptor preHandle....");
            //用户未登录，进行跳转。
            /*if (!defaultURl.startsWith("/")){
                defaultURl="/"+defaultURl;
            }*/
            response.sendRedirect("/sign-in.html");
            return false;
        }
        return true;
    }

}